//
//  CommodityDetailsView.m
//  yonghu
//
//  Created by 浪尖渝力 on 2018/2/7.
//  Copyright © 2018年 com.DJS. All rights reserved.
//

#import "CommodityDetailsView.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
#import "WyzAlbumViewController.h"
@implementation CommodityDetailsView

-(void)creatView{
    self.backgroundColor=_LightGrey;
    _scrollView=[UIScrollView new];
    [self addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    _scrollView.scrollsToTop =NO;
    
    _scrollView.showsVerticalScrollIndicator = FALSE;
    _scrollView.showsHorizontalScrollIndicator = FALSE;
    _scrollView.bounces = NO;
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.viewController.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
    NSMutableArray *imageArr=[NSMutableArray arrayWithCapacity:0];
    NSArray *goods_image=[_dic objectForKey:@"goods_image"];
    for (int i=0; i<goods_image.count; i++) {
        NSString *imageStr=goods_image[i];
        imageStr=[NSString stringWithFormat:@"%@%@",_addressUrl,imageStr];
        [imageArr addObject:imageStr];
    }
    
    UIView *imageView=[UIView new];
    [_scrollView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(64);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(_mainH*0.45);
    }];
    imageView.backgroundColor=_LightGrey;
    
    [ZQTools getHederImage:UIViewContentModeScaleAspectFit :CGRectMake(0, 0, _mainW,  _mainH*0.45) :imageArr :10 :imageView :^(int value) {
        

        WyzAlbumViewController *wyzAlbumVC = [[WyzAlbumViewController alloc]init];
        
        wyzAlbumVC.currentIndex =value;//这个参数表示当前图片的index，默认是0
        
        //图片数组，可以是url，也可以是UIImage
        //第一种用url
        wyzAlbumVC.imgArr = imageArr;
        wyzAlbumVC.imageNameArray=nil;//图片名字数组可以为空
        //进入动画
        
        [self.viewController presentViewController:wyzAlbumVC animated:YES completion:^{
            
            
        }];
        
    }];
    
    
    NSString *goods_introduce=[_dic objectForKey:@"goods_introduce"];
    CGSize size=[ZQTools getNSStringSize:goods_introduce andViewWight:_mainW-40 andFont:16];
    
    UIView *topView=[UIView new];
    [_scrollView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(imageView);
        make.top.mas_equalTo(imageView.mas_bottom);
        make.height.mas_equalTo(size.height+200);
    }];
    topView.backgroundColor=[UIColor whiteColor];
    
    _scrollView.contentSize = CGSizeMake(0,_mainH*0.45+size.height+210);
    
    UILabel *label=[UILabel new];
    [topView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(15);
    }];
    label.text=[_dic objectForKey:@"goods_name"];
    label.font=[UIFont systemFontOfSize:19];
    label.textColor=ColorWithRGB(29, 29,29);
    label.numberOfLines=0;
    
    UILabel *label2=[UILabel new];
    [topView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(label.mas_bottom).mas_equalTo(20);
    }];
    label2.text=goods_introduce;
    label2.font=[UIFont systemFontOfSize:16];
    label2.textColor=ColorWithRGB(149, 149,149);
    label2.numberOfLines=0;
    
    UIView *XianView=[UIView new];
    [topView addSubview:XianView];
    [XianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-50);
        make.height.mas_equalTo(1);
    }];
    XianView.backgroundColor=_LightGrey;
    
    UIView *bottomView=[UIView new];
    [topView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    NSNumber *goods_price=[_dic objectForKey:@"goods_price"];
    
    UILabel *label3=[UILabel new];
    [bottomView addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label);
        make.centerY.mas_equalTo(0);
    }];
    label3.text=[NSString stringWithFormat:@"¥%@/份",goods_price];
    label3.textColor=[UIColor redColor];
    label3.font=[UIFont systemFontOfSize:17];
    
    NSNumber *goods_sales=[_dic objectForKey:@"goods_sales"];
    
    UILabel *lable4=[UILabel new];
    [bottomView addSubview:lable4];
    [lable4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    lable4.text=[NSString stringWithFormat:@"销量:%@",goods_sales];
    lable4.textColor=ColorWithRGB(149, 149, 149);
    lable4.font=[UIFont systemFontOfSize:17];
    
    UIButton *btn=[UIButton new];
    [bottomView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(70);
    }];
    btn.backgroundColor=_backgroundColor;
    btn.layer.cornerRadius=5;
    [btn setTitle:@"购买" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn addTarget:self action:@selector(gouMai:) forControlEvents:UIControlEventTouchUpInside];
}

-(IBAction)gouMai:(id)sender{
   
    _view=[UIView new];
    [self addSubview:_view];
    [_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(_mainH);
    }];
    _view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    UIView *bottomView=[UIView new];
    [_view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(420);
    }];
    bottomView.backgroundColor=[UIColor whiteColor];
    
    UIView *view12=[UIView new];
    [_view addSubview:view12];
    [view12 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bottomView.mas_top);
        make.width.mas_equalTo(_mainW);
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
    }];
    view12.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture12 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(quXiao:)];
    [view12 addGestureRecognizer:tapGesture12];
    
    UIView *titleView=[UIView new];
    [bottomView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    UILabel *label=[UILabel new];
    [titleView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    label.text=@"确认付款";
    label.font=[UIFont systemFontOfSize:18];
    label.textColor=ColorWithRGB(49, 49, 49);
    
    UIView *xianView=[UIView new];
    [bottomView addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(titleView.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    xianView.backgroundColor=_LightGrey;
    
    UIView *view2=[UIView new];
    [bottomView addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(130);
        make.top.mas_equalTo(xianView.mas_bottom);
    }];
    NSNumber *goods_price=[_dic objectForKey:@"goods_price"];
    UILabel *label2=[UILabel new];
    [view2 addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(30);
    }];
    label2.text=[NSString stringWithFormat:@"¥%@",goods_price];
    label2.font=[UIFont systemFontOfSize:22];
    label2.textColor=_backgroundColor;
    
    UILabel *label3=[UILabel new];
    [view2 addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(20);
        make.width.mas_equalTo(_mainW-40);
    }];
    label3.text=[_dic objectForKey:@"goods_name"];
    label3.textColor=ColorWithRGB(149, 149, 149);
    label3.font=[UIFont systemFontOfSize:18];
    label3.textAlignment=NSTextAlignmentCenter;
    
    UIView *xianView2=[UIView new];
    [bottomView addSubview:xianView2];
    [xianView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(view2.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    xianView2.backgroundColor=_LightGrey;
    
    
    UIView *payView=[UIView new];
    [_view addSubview:payView];
    [payView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(xianView2.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(140);
    }];
    payView.backgroundColor=[UIColor whiteColor];
    payView.layer.cornerRadius=4;
    
    UIImageView *ZFBimageView=[UIImageView new];
    [payView addSubview:ZFBimageView];
    [ZFBimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(40);
    }];
    ZFBimageView.image=[UIImage imageNamed:@"支付宝支付"];
    
    UILabel *ZFBlabe=[UILabel new];
    [payView addSubview:ZFBlabe];
    [ZFBlabe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ZFBimageView.mas_right).mas_equalTo(10);
        make.centerY.mas_equalTo(ZFBimageView);
    }];
    ZFBlabe.text=@"支付宝支付";
    ZFBlabe.font=[UIFont systemFontOfSize:15];
    ZFBlabe.textColor=ColorWithRGB(149, 149, 149);
    
    
    _selectPay=1;
    _ZFBBtn=[UIImageView new];
    [payView addSubview:_ZFBBtn];
    [_ZFBBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.width.height.mas_equalTo(20);
        make.centerY.mas_equalTo(ZFBimageView);
    }];
    _ZFBBtn.tag=1;
    _ZFBBtn.image =[UIImage imageNamed:@"是"];
    _ZFBBtn.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPay:)];
    [_ZFBBtn addGestureRecognizer:tapGesture];
    
    UIImageView *WXimageView=[UIImageView new];
    [payView addSubview:WXimageView];
    [WXimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(ZFBimageView.mas_bottom).mas_equalTo(25);
        make.width.height.mas_equalTo(40);
    }];
    WXimageView.image=[UIImage imageNamed:@"微信好友"];
    
    UILabel *WXlabe=[UILabel new];
    [WXimageView addSubview:WXlabe];
    [WXlabe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(WXimageView.mas_right).mas_equalTo(10);
        make.centerY.mas_equalTo(WXimageView);
    }];
    WXlabe.text=@"微信支付";
    WXlabe.font=[UIFont systemFontOfSize:15];
    WXlabe.textColor=ColorWithRGB(149, 149, 149);
    
    
    _WXBtn=[UIImageView new];
    [payView addSubview:_WXBtn];
    [_WXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.width.height.mas_equalTo(20);
        make.centerY.mas_equalTo(WXimageView);
    }];
    _WXBtn.tag=2;
    _WXBtn.image=[UIImage imageNamed:@"否"];
    
    _WXBtn.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPay:)];
    [_WXBtn addGestureRecognizer:tapGesture2];

    UIView *xianView3=[UIView new];
    [bottomView addSubview:xianView3];
    [xianView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(payView.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    xianView3.backgroundColor=_LightGrey;
    
    UIView *btnView=[UIView new];
    [bottomView addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(xianView3.mas_bottom);
    }];
    
    UIButton *btn=[UIButton new];
    [btnView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(_mainW-40);
    }];
    btn.backgroundColor=_backgroundColor;
    btn.layer.cornerRadius=15;
    [btn setTitle:@"确认支付" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn addTarget:self action:@selector(payBtn:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)quXiao:(UITapGestureRecognizer *)tap{
    [_view removeFromSuperview];
}
-(IBAction)selectPay:(UITapGestureRecognizer *)tap{
    _selectPay=(int)tap.view.tag;
    if (_selectPay==1) {
        _ZFBBtn.image=[UIImage imageNamed:@"是"];
        _WXBtn.image=[UIImage imageNamed:@"否"];
    }
    else{
        _WXBtn.image=[UIImage imageNamed:@"是"];
        _ZFBBtn.image=[UIImage imageNamed:@"否"];
    }
}
-(IBAction)payBtn:(id)sender{
    [_view removeFromSuperview];
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    NSString *token=[userDic objectForKey:@"token"];
    NSNumber *waiter_id1=[userDic objectForKey:@"waiter_id"];
    NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
    
    NSNumber *dt_id1=[_dic objectForKey:@"goods_id"];
    NSString *goods_id=[NSString stringWithFormat:@"%@",dt_id1];
    
    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",@"1",@"user_role",user_id,@"waiter_id",goods_id,@"goods_id",[NSString stringWithFormat:@"%d",_selectPay],@"pay_type", nil];
    [ZQTools AFNPostDataUrl:@"waiter/account/pay" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        if (_selectPay==1) {
            [self diaoZhiFuBao:[responseObject objectForKey:@"aliContent"]];
        }
        else{
            [self WeiXin:responseObject];
        }
    } anderrorBlock:nil  ];
    
}
-(void)diaoZhiFuBao:(NSString *)order{
    
    
    
    NSString *appScheme = @"DJSFWPaydemo";
    [[AlipaySDK defaultService] payOrder:order fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        if ([resultDic[@"resultStatus"]intValue] == 9000) {
            
        } else {
            [ZQTools svpInfo:@"支付失败"];
        }
    }];
}
-(void)WeiXin:(NSDictionary *)sender{
    NSUserDefaults *zhiFu = [NSUserDefaults standardUserDefaults];
    [zhiFu setObject:@"weinXin" forKey:@"ZhiFu"];
    PayReq *request = [[PayReq alloc] init];
    /** 商家向财付通申请的商家id */
    request.partnerId =[sender objectForKey:@"partnerid"];
    /** 预支付订单 */
    request.prepayId=[sender objectForKey:@"prepayid"];
    /** 商家根据财付通文档填写的数据和签名 */
    request.package =@"Sign=WXPay";
    /** 随机串，防重发 */
    request.nonceStr= [sender objectForKey:@"noncestr"];
    /** 时间戳，防重发 */
    request.timeStamp=[[sender objectForKey:@"timestamp"] intValue];
    /** 商家根据微信开放平台文档对数据做的签名 */
    request.sign= [sender objectForKey:@"sign"];
    /*! @brief 发送请求到微信，等待微信返回onResp
     *
     * 函数调用后，会切换到微信的界面。第三方应用程序等待微信返回onResp。微信在异步处理完成后一定会调用onResp。支持以下类型
     * SendAuthReq、SendMessageToWXReq、PayReq等。
     * @param req 具体的发送请求，在调用函数后，请自己释放。
     * @return 成功返回YES，失败返回NO。
     */
    
    if ([WXApi sendReq: request]) {
        
        
//        NSNotification * notice = [NSNotification notificationWithName:@"zhiFu" object:nil userInfo:nil];
//        //发送消息
//        [[NSNotificationCenter defaultCenter]postNotification:notice];
//
//        NSNotificationCenter * center2 = [NSNotificationCenter defaultCenter];
//        //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
//        [center2 addObserver:self selector:@selector(networkDidReceiveMessage2:) name:@"zhiFu2" object:nil];
        
        
    }
    
    
    
    
}

@end
